part of 'sounds_button.dart';

class JzxRecordingStatusMaskView extends StatefulWidget {
  const JzxRecordingStatusMaskView(
    this.polymerData, {
    super.key,
    this.onCancelSend,
    this.onVoiceSend,
  });

  final PolymerData polymerData;

  /// 取消发送
  final VoidCallback? onCancelSend;

  /// 原音发送
  final VoidCallback? onVoiceSend;

  @override
  State<JzxRecordingStatusMaskView> createState() =>
      _JzxRecordingStatusMaskViewState();
}

class _JzxRecordingStatusMaskViewState
    extends State<JzxRecordingStatusMaskView> {
  /// 屏幕大小
  final scSize = Size(ScreenUtil().screenWidth, ScreenUtil().screenHeight);
  double paddingSide = 0;

  /// 录音

  @override
  void initState() {
    super.initState();
    paddingSide =
        (ScreenUtil().screenWidth - widget.polymerData.data.iconFocusSize * 3) /
            3;
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    final data = widget.polymerData.data;
    final soundsRecorder = widget.polymerData.controller;

    return Material(
      // type: MaterialType.transparency,
      color: Colors.black.withOpacity(0.7),
      // color: Colors.transparent,
      child: PolymerState(
        data: widget.polymerData,
        child: ValueListenableBuilder(
          valueListenable: widget.polymerData.controller.status,
          builder: (context, value, child) {
            return _MaskStackView(
              onTapClose: widget.onCancelSend,
              children: [
                _Bubble(
                  paddingSide: paddingSide,
                ),
                Column(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    Text(value.title, style: data.maskTxtStyle),
                    const SizedBox(height: 18),
                    CustomPaint(
                      // size: Size(double.infinity, data.sendAreaHeight),
                      painter: _RecordingPainter(
                          value == SoundsMessageStatus.recording),
                      child: Container(
                        width: double.infinity,
                        height: data.sendAreaHeight,
                        alignment: Alignment.center,
                        child: GestureDetector(
                          onLongPress: () async {
                            // 录制
                            soundsRecorder.startOrStopRecording(
                              onDurationChanged: (time) {
                                debugPrint(
                                    '________  onDurationChanged: $time ');
                              },
                              onCompleted: (path, time) {
                                // _removeMask();

                                debugPrint(
                                    '________  onCompleted: $path , $time ');

                                if (time < 1 &&
                                    soundsRecorder.status.value ==
                                        SoundsMessageStatus.recording) {
                                  showMessage(
                                      'The recording time is too short');
                                  return;
                                }

                                // 录制状态松开/超过时长 直接发送语音内容
                                if (soundsRecorder.status.value ==
                                    SoundsMessageStatus.recording) {
                                  widget.onVoiceSend?.call();
                                }
                                // 取消发送
                                else if (soundsRecorder.status.value ==
                                    SoundsMessageStatus.canceling) {
                                  widget.onCancelSend?.call();
                                }
                              },
                            );
                          },
                          onLongPressMoveUpdate: (details) {
                            // 录音状态下的手势移动处理
                            if (soundsRecorder.status.value ==
                                SoundsMessageStatus.none) {
                              return;
                            }
                            final offset = details.globalPosition;
                            if ((scSize.height - offset.dy.abs()) >
                                widget.polymerData.data.sendAreaHeight) {
                              soundsRecorder
                                  .updateStatus(SoundsMessageStatus.canceling);
                            } else {
                              soundsRecorder
                                  .updateStatus(SoundsMessageStatus.recording);
                            }
                          },
                          onLongPressEnd: (details) async {
                            // 手势结束音频
                            soundsRecorder.endRec();
                          },
                          child: value == SoundsMessageStatus.recording
                              ? Assets.images.icVoiceRecording.image(width: 64)
                              : Assets.images.icVoiceCancel.image(width: 64),
                        ),
                      ),
                    ),
                  ],
                ),
              ],
            );
          },
        ),
      ),
    );
  }
}
